package com.george.shiro.model.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.george.shiro.model.entity.Resources;
import com.george.shiro.model.dao.ResourcesMapper;
import com.george.shiro.model.service.IResourcesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.george.shiro.vo.ResourceConditionVO;
import com.george.shiro.vo.ResourcesVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author george
 * @since 2019-05-21
 */
@Service
public class ResourcesServiceImpl extends ServiceImpl<ResourcesMapper, Resources> implements IResourcesService {

    @Override
    public List<Resources> findAllResources() {
        List<Resources> resources = this.baseMapper.selectAllResources();
        return resources;
    }

    @Override
    public List<Resources> findResourcesByUserId(Long userId) {
        List<Resources> resources = this.baseMapper.selectResourcesByUserId(userId);
        return resources;
    }

    @Override
    public List<Resources> findUrlAndPermision() {
        List<Resources> resourcesList = this.baseMapper.selectUrlAndPermision();
        if (CollectionUtil.isNotEmpty(resourcesList)) {
            return resourcesList;
        }
        return Collections.EMPTY_LIST;
    }

    @Override
    public List<ResourcesVo> listUserResources(Map<String, Object> params) {
        List<ResourcesVo> resourcesVoList = this.baseMapper.getUserResources(params);
        return resourcesVoList;
    }

    @Override
    public List<ResourcesVo> listAllAvailableMenu() {
        List<ResourcesVo> resourcesVoList = this.baseMapper.getAllAvailableMenu();
        return resourcesVoList;
    }

    @Override
    public PageInfo<ResourcesVo> findPageBreakByCondition(ResourceConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<ResourcesVo> resources = this.baseMapper.findPageBreakByCondition(vo);
        PageInfo<ResourcesVo> pageInfo = new PageInfo<>(resources);
        pageInfo.setList(resources);
        return pageInfo;
    }

    @Override
    public List<Map<String, Object>> queryResourcesListWithSelected(Long rid) {
        List<ResourcesVo> sysResources = this.baseMapper.queryResourcesListWithSelected(rid);
        if (CollectionUtils.isEmpty(sysResources)) {
            return null;
        }
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        Map<String, Object> map;
        for (ResourcesVo resources : sysResources) {
            map = new HashMap<>(3);
            map.put("id", resources.getId());
            map.put("pId", resources.getParentId());
            map.put("checked", resources.getChecked());
            map.put("name", resources.getName());
            mapList.add(map);
        }
        return mapList;
    }
}
